// subPages/communityService/secondHandMall/secondHandMall.js
const app = getApp();
var utils = require('../../../utils/util');
var loadMoreView,p = 1,num = 4,type='goods'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      themeColor:app.globalData.themeColor,
      btnText:'搜索',
      placeholderText:'搜索您感兴趣的商品~',
      // expertsClassList:[{class_name: '全部'},{class_name:'最新发布'}],
      expertsClassCurrent: 0,
      iconNavList:[
          // {
          //     iconH:46,
          //     iconW:46,
          //     url:'/resources/communityService/icon06.png',
          //     id:1,
          //     background:'#ffbd3d',
          //     text:'电子数码'
          // },{
          //     background: '#00aeff',
          //     url:'/resources/communityService/icon06.png',
          //     iconW: 52,
          //     iconH: 46,
          //     text: '家具'
          //
          // },{
          //     background: '#ff3d9d',
          //     url:'/resources/communityService/icon06.png',
          //     iconW: 52,
          //     iconH: 46,
          //     text: '日常用品'
          // },{
          //     background: '#ff3d74',
          //     url:'/resources/communityService/icon06.png',
          //     iconW: 52,
          //     iconH: 46,
          //     text: '奢侈品'
          // }
          // ,{
          //     background: '#d43dff',
          //     url:'/resources/communityService/icon06.png',
          //     iconW: 52,
          //     iconH: 46,
          //     text: '家用电器'
          // }
      ],
      goodsList:[
          // {
          //     id:1,
          //     head_img:'https://jzj.freei.cc/Uploads/candelete/supermarket-img.png',
          //     name:'的交付定金客户',
          //     price:2000,
          //     address:'山东科技货款收',
          //     ykj:'0'
          // }
      ]
  },
    toSearchPage(){
        wx.navigateTo({
            url:'/subPages/communityService/searchPage/searchPage?type=0'
        })
    },
    goodsListEvent(e){
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
          url:'/subPages/communityService/secondHandMallDetails/secondHandMallDetails?id='+id
      })
    },
    toMySecondHandMall(){
        if(app.globalData.userId){
            wx.navigateTo({
                url:'/subPages/communityService/mySecondHandMall/mySecondHandMall'
            })
        }else{
            utils.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
                if (app.globalData.userInfo) {
                    //已授权
                    wx.redirectTo({
                        url: '/pages/login/login'
                    })
                }else{
                    // 在没有 open-type=getUserInfo 版本的兼容处理
                    if(app.globalData.isAuthorized){
                        app.userInfoReadyCallback = res => {
                          if (res) {
                            //已授权
                            wx.redirectTo({
                              url: '/pages/login/login'
                            })
                          }else{
                            //未授权
                            wx.navigateTo({
                              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                            })
                          }
                        }
                      }else{
                        wx.navigateTo({
                          url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                        })
                    }
                }
            })
        }
      
    },
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        let that = this;
        utils.setNavBarTitle('二手商城');
        loadMoreView = this.selectComponent("#loadMoreView");
        this.getClass();
        this.getItems();
        // console.log(app.globalData.userId);
        // that.getHomemakingService();
    },
    iconNavBtn(e){
        let id = e.currentTarget.dataset.id;
        // console.log(id);
        wx.navigateTo({
            url:'/subPages/communityService/secondHandMallItems/secondHandMallItems?id='+id
        })
    },
    getClass(){
      let _this = this;
      utils.getAjax(`${app.globalData.apiUrl}/get_old_product_class`,function(res){
          // console.log(res);
          for(var i in res.data){
              res.data[i].text = res.data[i].class_name;
              res.data[i].url = res.data[i].ico;
              res.data[i].iconW = '46';
              res.data[i].iconH = '46';
              res.data[i].background = '#ff3d9d'
          }
          _this.setData({
              iconNavList:res.data
          })
      },{
          community_id:app.globalData.communityId
      })
    },
    getItems(){
      let _this = this;
      utils.getAjax(`${app.globalData.apiUrl}/get_old_product_all`,res=>{
          // console.log(res);
          if(res.data.list.length != 0){
              var list = [];
              if(p == 1){
                  list = res.data.list;
              }else{
                  list = _this.data.goodsList.concat(res.data.list)
              }
              _this.setData({
                  goodsList:list
              })
          }else{
            _this.setData({
                goodsList:[]
            })
          }
          loadMoreView.loadMoreComplete(res.data,type);
      },{
          p:p,
          num:num,
          community_id:app.globalData.communityId,
          status:10
      })
    },

    //获取服务数据异常
    shopDataErr(err){
        if(p>1){
            loadMoreView.loadMoreFail()
        }else{
            util.showModal('提示','获取商品失败，请向上滑动屏幕')
            this.setData({
                isFail: true
            })
        }
    },
    loadMoreListener: function(e) {
        p += 1
        this.getItems()
    },
    //
    clickLoadMore: function(e) {
        this.getItems()
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.isFail){
          this.getItems()
          return
      }
      loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
